package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.model.DramaCategoryModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.DramaCategoryListActivity;
import com.hangzhou.xyj.ui.activity.ExpertListActivity;
import com.hangzhou.xyj.ui.widget.FamousExpertBannerView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DT-038 on 2015/12/22.
 * 名团adapter
 */
public class FamousTeamAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    OnCategroyChangeListener mListener;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<BannerModel> mBannerList;
    private List<DramaCategoryModel> mCategoryList;
    private List<UserModel> mFamousTeamList;//名团数据
    private int curCatId;//当前选中的剧种的Id
    private String curCatName;//当前选中的剧种的Name

    public FamousTeamAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * 设置当前选中的剧种的Id
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/14 15:16
     */
    public void setCurCatId(int curCatId) {
        this.curCatId = curCatId;
    }

    /**
     * 设置当前选中的剧种的Name
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/14 15:17
     */
    public void setCurCatName(String curCatName) {
        this.curCatName = curCatName;
    }

    public void setOnCategroyChangeListener(OnCategroyChangeListener listener) {
        this.mListener = listener;
    }

    /**
     * 更新banner条
     *
     * @param bannerList
     */
    public void updateBanner(List<BannerModel> bannerList) {
        if (bannerList == null) {
            bannerList = new ArrayList<>();
        }
        this.mBannerList = bannerList;
        notifyDataSetChanged();
    }

    /**
     * 更新剧种数据
     *
     * @param categoryList
     */
    public void updateCategory(List<DramaCategoryModel> categoryList) {
        if (categoryList == null) {
            categoryList = new ArrayList<>();
        }
        this.mCategoryList = categoryList;
        notifyDataSetChanged();
    }

    /**
     * 更新选中剧种的名团信息
     *
     * @param userModelList
     */
    public void updateUserByCat(List<UserModel> userModelList) {
        if (userModelList == null) {
            userModelList = new ArrayList<>();
        }
        this.mFamousTeamList = userModelList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            case 0:
                viewHolder = new BannerViewHolder(mLayoutInflater.inflate(R.layout.layout_banner, viewGroup, false));
                break;
            case 1:
                viewHolder = new DramaViewHolder(mLayoutInflater.inflate(R.layout.layout_drama_custom_grid, viewGroup, false));
                break;
            case 2:
                viewHolder = new FamousTeamHolder(mLayoutInflater.inflate(R.layout.layout_custom_grid, viewGroup, false));
                break;
            default:
                viewHolder = new FamousTeamHolder(mLayoutInflater.inflate(R.layout.layout_custom_grid, viewGroup, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int viewType) {
        if (viewHolder == null) {
            return;
        }
        if (viewHolder instanceof BannerViewHolder) {
            ((BannerViewHolder) viewHolder).setData();
        } else if (viewHolder instanceof DramaViewHolder) {
            ((DramaViewHolder) viewHolder).setData();
        } else if (viewHolder instanceof FamousTeamHolder) {
            ((FamousTeamHolder) viewHolder).setData();
        }

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    public interface OnCategroyChangeListener {
        void updateUserByCat(int categoryId);
    }

    /**
     * Banner的ViewHolder
     */
    class BannerViewHolder extends RecyclerView.ViewHolder {
        private FamousExpertBannerView mItemView;

        public BannerViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View view) {
            mItemView = (FamousExpertBannerView) view.findViewById(R.id.view_famous_expert_banner);
        }

        public void setData() {
            mItemView.setData(mBannerList);
        }
    }

    /**
     * 剧种的ViewHolder
     */
    class DramaViewHolder extends RecyclerView.ViewHolder {
        private GridView mGridView;
        private DramaAdapter mAdapter;

        public DramaViewHolder(View itemView) {
            super(itemView);
            mGridView = (GridView) itemView.findViewById(R.id.grid_view);
            mGridView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapter, View view,
                                        int position, long id) {
                    //如果点击的是更多则跳转
                    if (position == 9) {
                        Intent intent = new Intent(mContext, DramaCategoryListActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_STAGE_TEAM.getValue());
                        mContext.startActivity(intent);
                        return;
                    }
                    //刷新选中颜色,当前为选中色，其他为未选中
                    for (int i = 0; i < mCategoryList.size(); i++) {
                        if (i == position) {
                            mCategoryList.get(i).setIsSelected(true);
                        } else {
                            mCategoryList.get(i).setIsSelected(false);
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                    curCatId = mCategoryList.get(position).getId();
                    curCatName = mCategoryList.get(position).getName() + "-名团";
                    //更新名团数据
                    mListener.updateUserByCat(curCatId);
                }
            });
        }

        public void setData() {
            mGridView.setAdapter(mAdapter = new DramaAdapter(mContext, mCategoryList));
        }
    }

    /**
     * 名团ViewHolder
     */
    class FamousTeamHolder extends RecyclerView.ViewHolder {
        private GridView mGridView;
        private Intent mIntent;

        public FamousTeamHolder(View itemView) {
            super(itemView);
            mGridView = (GridView) itemView.findViewById(R.id.grid_view);
            mGridView.setBackgroundResource(R.color.common_bg);

            mGridView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapter, View view,
                                        int position, long id) {
                    //如果点击的是更多则跳转
                    if (position == 7) {
                        mIntent = new Intent(mContext, ExpertListActivity.class);
                        mIntent.putExtra(DataKey.TITLE, curCatName);
                        mIntent.putExtra(DataKey.INDEX, curCatId);
                        mIntent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_STAGE_TEAM.getValue());
                        IntentManager.getInstance().goExpertListActivity(mContext, mIntent);
                    } else {
                        IntentManager.getInstance().goExpertInfoActivity(mContext, Integer.valueOf(mFamousTeamList.get(position).getUserId()),
                                EIntentTagType.TAG_STAGE_TEAM);
                    }
                }
            });
        }

        public void setData() {
            mGridView.setAdapter(new FamousAdapter(mContext, mFamousTeamList));
        }
    }
}
